//
//  HJComposePictureView.swift
//  Weibo
//
//  Created by 韩佳 on 16/4/26.
//  Copyright © 2016年 ddg. All rights reserved.
//

import UIKit
import SVProgressHUD

class HJComposePictureView: UICollectionView {

    lazy var images:[UIImage] = []
    var selectItemClosure:(()->())?
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        setupUI()
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI(){
        backgroundColor = UIColor.whiteColor()
        registerClass(HJComposePictureViewCell.self, forCellWithReuseIdentifier: "cell")
        self.dataSource = self
        self.delegate = self
    }
    //提供外界一个添加图片的方法
    func addImage(image: UIImage){
        if images.count < 9{
            images.append(image)
            reloadData()
        }else{
            SVProgressHUD.showErrorWithStatus("图片数量已达上限")
        }
    }
    override func layoutSubviews() {
        super.layoutSubviews()

        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        let margin: CGFloat = 5
        let itemHW = (self.frame.width - 2 * margin) / 3
        layout.itemSize = CGSizeMake(itemHW, itemHW)
        layout.minimumInteritemSpacing = margin
        layout.minimumLineSpacing = margin
        
    }
}

extension HJComposePictureView:UICollectionViewDataSource, UICollectionViewDelegate{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //没图时设置隐藏pickerView
        self.hidden = images.count == 0
        //当图片不是9张时加一个item显示加号图片
        return images.count + (images.count == 0 || images.count == 9 ? 0 : 1)
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! HJComposePictureViewCell
//        cell.backgroundColor = RandomColor
        if indexPath.item == images.count{
            cell.image = nil
        }else{
            cell.image = images[indexPath.item]
        }
        cell.deleteBtnClosure = { [weak self] in
            self?.images.removeAtIndex(indexPath.item)
            self?.reloadData()
        }
        return cell
    }
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        if indexPath.item == self.images.count{
            selectItemClosure?()
        }
    }
}

class HJComposePictureViewCell: UICollectionViewCell {
    
    var deleteBtnClosure:(()->())?
    var image:UIImage?{
        didSet{
            if image == nil{
                imgView.image = UIImage(named: "compose_pic_add")
                deleteBtn.hidden = true
            }else{
                deleteBtn.hidden = false
                imgView.image = image
            }
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI(){
        contentView.addSubview(imgView)
        contentView.addSubview(deleteBtn)
        imgView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView)
        }
        deleteBtn.snp_makeConstraints { (make) -> Void in
            make.top.trailing.equalTo(contentView)
        }
    }
    @objc private func deleteBtnClick(){
        deleteBtnClosure?()
    }
    private lazy var imgView: UIImageView = UIImageView()
    private lazy var deleteBtn: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "deleteBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
}



